package com.xuan.system.manager;

import com.xuan.common.core.constants.CacheConstants;
import com.xuan.common.redis.service.RedisService;
import com.xuan.system.domain.user.entity.User;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


@Component
public class UserCacheManager {


    @Resource
    private RedisService redisService;


    public void updateUserStatus(Long userId, Integer status) {
        // 判断 C 端是否已经获取过
        // 若缓存中不存在则不更新(原因是 C 端接口中只有用户详情接口和其他部分接口需要判断用户是否被拉黑)
        String userDetailKey = getUserDetailKey(userId);
        User user = redisService.getCacheObject(userDetailKey, User.class);
        if (user == null) {
            return;
        }

        // 刷新用户详情缓存,设置 10 分钟过期时间
        user.setStatus(status);
        redisService.setCacheObject(userDetailKey, user, CacheConstants.USER_EXP, TimeUnit.MINUTES);
    }

    private String getUserDetailKey(Long userId) {
        return CacheConstants.USER_DETAIL_KEY + userId;
    }
}
